package 力扣日常刷题.木22二月.第01天206;

import 力扣日常刷题.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/2/6
 * @description
 */
public class Demo07JZ从上到下打印二叉树 {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) return res;
        bfs(root);
        return res;
    }

    void bfs(TreeNode node) {
        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        queue.add(node);

        while (!queue.isEmpty()) {
            ArrayList<Integer> list = new ArrayList<>();
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                TreeNode pop = queue.pop();
                if (pop.left != null) queue.add(pop.left);
                if (pop.right != null) queue.add(pop.right);
                list.add(pop.val);
            }
            res.add(list);
        }

    }



}
